import * as THREE from 'three'
import gsap from 'gsap'
import vertexShader from '@/shader/LightRadar/vertex.glsl'
import fragmentShader from '@/shader/LightRadar/fragment.glsl'

// 修改材质
export default class LightRadar {
  constructor(radius = 2, position = { x: -1.8, z: 3 }, color = 0xffff00) {
    this.geometry = new THREE.PlaneGeometry(radius, radius)

    this.material = new THREE.ShaderMaterial({
      uniforms: {
        uColor: { value: new THREE.Color(color) },
        uTime: { value: 0 },
      },
      vertexShader,
      fragmentShader,
      transparent: true,
      side: THREE.DoubleSide,
    })
    this.mesh = new THREE.Mesh(this.geometry, this.material)
    this.mesh.position.set(position.x, 1, position.z)
    this.mesh.rotateX(-Math.PI / 2)

    gsap.to(this.material.uniforms.uTime, {
      value: 1,
      duration: 1,
      ease: 'none',
      repeat: -1,
    })
  }

  remove() {
    this.mesh.remove()
    this.mesh.removeFromParent()
    this.mesh.geometry.dispose()
    this.mesh.material.dispose()
  }
}
